package com.manager.api.controller;

import com.manager.base.BaseResponse;
import com.manager.controller.BaseController;
import com.manager.entity.Projects;
import com.manager.service.IProjectsService;
import com.manager.util.StringUtils;
import com.manager.vo.ProjectsVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @className: ProjectsController
 * @description: TODO 类描述
 * @author: tao
 * @date: 2021/7/18 16:37
**/
@RestController
@RequestMapping("/api")
public class ProjectsController extends BaseController {
  @Resource
  private IProjectsService iProjectsService;

  /**
   *
   * @className: ProjectsController
   * @description: 查询项目
   * @author: tao
   * @date: 2021/7/18 16:37
  **/
  @GetMapping("/project")
  public BaseResponse findProjectAll(Projects projects) {
    // 将项目查出来并进行转换
    List<ProjectsVo> resList = iProjectsService.selectProjectsList(projects)
      .stream().map((project) -> {
        ProjectsVo projectsVo = new ProjectsVo();
        projectsVo.setId(project.getId());
        projectsVo.setName(project.getName());
        projectsVo.setIsProject(project.getIsProject());
        projectsVo.setDesc(project.getDesc());
        projectsVo.setLock(project.getLock());
        projectsVo.setCreatedTime(project.getCreatedTime());
        projectsVo.setUpdatedTime(project.getUpdatedTime());
        return projectsVo;
      }).collect(Collectors.toList());;

    return BaseResponse.success("操作成功", resList);
  }

  /**
   *
   * @className: ProjectsController
   * @description: 创建项目
   * @author: tao
   * @date: 2021/7/18 16:40
  **/
  @PostMapping("/project")
  @ResponseBody
  public BaseResponse creatProject(@Valid @RequestBody ProjectsVo projectsVo) {
    Projects projects = new Projects();
    iProjectsService.insertProjects(toProject(projects, projectsVo));
    return BaseResponse.success("操作成功","");
  }

  /**
   *
   * @className: ProjectsController
   * @description: 修改项目
   * @author: tao
   * @date: 2021/7/18 16:40
   **/
  @PatchMapping("/project/{projectId}")
  @ResponseBody
  public BaseResponse updateProject(@PathVariable("projectId") Long projectId, @Valid @RequestBody ProjectsVo projectsVo) {
    if(StringUtils.isNull(projectId)) {
      return BaseResponse.error("修改失败","");
    }
    Projects projects = new Projects();
    projects.setId(projectId);
    iProjectsService.updateProjects(toProject(projects, projectsVo));
    return BaseResponse.success("修改成功","");
  }

  /**
   *
   * @className: ProjectsController
   * @description: 删除项目(单个删除)
   * @author: tao
   * @date: 2021/7/18 16:40
   **/
  @DeleteMapping("/project/{projectId}")
  @ResponseBody
  public BaseResponse deleteProject(@PathVariable("projectId") Long projectId) {
    if(StringUtils.isNull(projectId)) {
      return BaseResponse.error("操作失败","");
    }
    Projects projects = new Projects();
    projects.setId(projectId);
    projects.setStatus(1);
    iProjectsService.updateProjects(projects);
    return BaseResponse.success("删除成功","");
  }

  /**
   * Vo转换实体类
   * @author: tao
   * @date: 2021/7/18 18:18
   * @param projects 项目对象
   * @param projectsVo 项目对象Vo
   * @return:  projects 项目对象
  **/
  private Projects toProject(Projects projects, ProjectsVo projectsVo) {
    projects.setUserId(projectsVo.getUserId());
    projects.setIsProject(projectsVo.getIsProject());
    projects.setName(projectsVo.getName());
    projects.setDesc(projectsVo.getDesc());
    projects.setLock(projectsVo.getLock());
    projects.setUpdatedTime(new Date());
    projects.setCreatedTime(new Date());
    return projects;
  }

}
